package app.recycler.com.recycler.ui;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;

import com.yanzhenjie.permission.Action;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Permission;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import app.recycler.com.recycler.R;
import app.recycler.com.recycler.contacts.BaseDataSource;
import app.recycler.com.recycler.contacts.ContactRVAdapter;
import app.recycler.com.recycler.contacts.ContactsInfo;
import app.recycler.com.recycler.contacts.ContactsInfoHelper;
import app.recycler.com.recycler.contacts.UploadContactBean;

public class ContactsActivity extends AppCompatActivity {
    public static final String KEY_CONTACTS_TAG = "key_contacts_tag";

    private RecyclerView mRv;
    private ContactRVAdapter mAdapter;
    private List<UploadContactBean> contactList = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contacts);
        setTitle("选取联系人");
        mRv = findViewById(R.id.rv);
        AndPermission.with(ContactsActivity.this)
                .runtime().permission(Permission.Group.CONTACTS)
                .onGranted(new Action<List<String>>() {
                    @Override
                    public void onAction(List<String> data) {
                        getContactsList();
                    }
                }).onDenied(new Action<List<String>>() {
            @Override
            public void onAction(List<String> data) {
            }
        }).start();
    }

    public void getContactsList() {
        ContactsInfoHelper.getInstance().getContactList(new BaseDataSource.GetDataSourceCallback<List<ContactsInfo>>() {
            @Override
            public void onLoaded(@NonNull List<ContactsInfo> data) {
                for (ContactsInfo contactsInfo : data) {
                    if (contactsInfo != null && (!(TextUtils.isEmpty(contactsInfo.getName()))) && validPhoneNumber(contactsInfo.getPhone())) {
                        UploadContactBean uploadContactBean =
                                new UploadContactBean(contactsInfo.getName(), contactsInfo.getPhone());
                        contactList.add(uploadContactBean);
                    }
                }
                Collections.sort(contactList);
                initRecyclerView();
            }

            @Override
            public void onDataNotAvailable() {
            }
        });
    }

    public void initRecyclerView() {
        if (mRv != null) {
            mRv.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
            mAdapter = new ContactRVAdapter(contactList);
            mRv.setAdapter(mAdapter);
        }
    }

    private boolean validPhoneNumber(String phoneNum) {
        return phoneNum.length() >= 4 && phoneNum.length() <= 17;
    }
}
